<?php 
include("confirmationAlert.php");
?>

<div id="booked-list-dlg" style="font-size: 62.5%; display: none;" title="List zapisanych">
	<p class="tips"></p>
	<table id="booked-list-table">
		<tbody>
			
		</tbody>
	</table>
</div>

<div id="edit-event-dlg" style="font-size: 62.5%; display: none;" title="Edycja rezerwacji">
	<form id="edit-event-form" method="post" action="<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/editKeeperReservation">
		<p class="validateTips"></p>
		<input type="hidden" name="id" class="id" >
		<label for="stationname">Stanowisko: </label>
		<input id="stationname" class="stationname" name="stationname" disabled="disabled">
		<label for="name">Nazwa: </label>
		<input id="eventname" class="eventname" name="name">
		<label for="startdate">Start rezerwacji: </label>
		<input disabled="disabled" class="startdate" id="startdate" name="startdate">
		<label for="enddate" id="enddate">Koniec rezerwacji: </label>
		<input disabled="disabled" class="enddate" name="enddate">
		
		<label for="limit">Limit studentów: </label>
		<input id="limit" class="limit" name="limit">
	</form>
</div>

<script>
function updateEditTips( t ) {
	var tips = $("#edit-event-dlg .validateTips");
	tips.text( t ).addClass( "ui-state-highlight" );
	setTimeout(function() {
		tips.removeClass( "ui-state-highlight", 1500 );
	}, 500 );
};

function validateEditTeacherEvent() {
	if ( !is_int($("#edit-event-dlg .limit").val()) ) {
		updateEditTips('Limit musi być liczbą całkowitą');
		return false;
	}
	if ( $("#edit-event-dlg .eventname").val().length < 1 ) {
		updateEditTips('Nazwa jest wymagana');
		return false;
	}
	return true;
}

$(function() {
	$( "#booked-list-dlg" ).dialog({
		autoOpen: false,
		height: 400,
		width: 350,
		modal: true,
		resizable: false,
		buttons : {
			"Ok" : function() {
				$( this ).dialog( "close" );
			}
		}
	});
	$( "#edit-event-dlg" ).dialog({
		autoOpen: false,
		height: 500,
		width: 350,
		modal: true,
		resizable: false,
		buttons : {
			"Zapisz" : function() {
				if ( validateEditTeacherEvent() ) {
					$.post(
						'<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/editTeacherReservation',
						$("#edit-event-form").serialize()).done(
		            					function(data) {
		            						$('#calendar').fullCalendar( 'refetchEvents' );
		            					});
					$( this ).dialog( "close" );
				}
			},
			"Lista zapisanych" : function() {
				var tips = $("#booked-list-dlg .tips");
				tips.html("Wczytywanie danych...");
				var tbody = $("#booked-list-table tbody");
				tbody.html("");
				$( "#booked-list-dlg" ).dialog("open");
				$.post(
					'<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/bookedStudents',
					{
						idTeacherReservation : $("#edit-event-form .id").val()
					},
					function(data) {
						if ( typeof (data.list) == 'undefined' ) {
							tips.html("Brak danych");
						}
						else if ( data.list.length == 0 ) {
							tips.html("Brak danych");
						}
						else {
							tips.html("");
							for ( var i = 0; i < data.list.length; i++ ) {
								tbody.append("<tr><td>" + (i + 1) + ".</td><td>"+data.list[i].name+"</td></tr>");
							}
						}
					},
					"json"
				);
			},
			"Usuń":function(){
				$.confirm({
		            'title'		: deleteAlert.title_begin + deleteAlert.topic_reservation + deleteAlert.title_end,
		            'message'	: deleteAlert.message,
		            'buttons'	: {
		                'Tak'	: {
		                    'class'	: 'blue',
		                    'action': function(){	
		                    	$.post(
		        						'<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/deleteTeacherReservation',
		        						$("#edit-event-form").serialize()).done(
		            					function(data) {
		            						$('#calendar').fullCalendar( 'refetchEvents' );
		            					});
								$( "#edit-event-dlg" ).dialog( "close" );
		                    }
		                },
		                'Nie'	: {
		                    'class'	: 'gray',
		                    'action': function(){
		                    	$( "#edit-event-dlg" ).dialog( "close" );
			                    }	// Nothing to do in this case. You can as well omit the action property.
		                }
		            }
		        });					
			},
			"Anuluj": function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			
		}
	});
});
</script>